//
//  ViewController.m
//  CodeGenerator
//
//  Created by 丁玉松 on 2017/8/31.
//  Copyright © 2017年 Beijing Yingyan Internet Co., Ltd. All rights reserved.
//

#import "ViewController.h"
#import "ModelGenerator.h"
#import "YYIVCTemplateViewController.h"
#import "APIGenerator.h"
#import "CellGenerator.h"
#import "DMGParameter.h"
#import "YYICellGeneratorViewController.h"


#define view_tag_addrequest     100

#define view_tag_selectfile     101

#define view_tag_addmodel       102

#define view_tag_vcmodel        103

#define view_tag_addmuban       104

#define view_tag_addcell        105



@interface ViewController ()<NSTableViewDelegate, NSTableViewDataSource>
@property (weak) IBOutlet NSComboBox *targetFileTF;

//@property (weak) IBOutlet NSTextField *targetFileTF;
@property (weak) IBOutlet NSTextField *descriptionTF;
@property (weak) IBOutlet NSTextField *requestNameTF;
@property (weak) IBOutlet NSTextField *urlTF;

@property (weak) IBOutlet NSTableView *requestTableView;
@property (weak) IBOutlet NSTableView *responseTableView;
//@property (weak) IBOutlet NSComboBox *comboBox;
@property (unsafe_unretained) IBOutlet NSTextView *textView;

@property (nonatomic, strong) NSMutableArray *requestDataSource;
@property (nonatomic, strong) NSMutableArray *responseDataSource;

@property (nonatomic, copy) NSString *filePath;
@property (nonatomic, copy) NSString *apiName;

/**
 请求参数字符串
 */
@property (nonatomic, copy) NSMutableString *requestMethodParamStr;
@property (nonatomic, copy) NSString *markMacro;

@property (nonatomic, copy) NSString *superCla;

@property (nonatomic ,strong) NSMutableArray *modelArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.requestDataSource = [NSMutableArray new];
    DMGParameter *pa = [DMGParameter new];
    pa.name = @"start";
    pa.prop = @"NSString";
    pa.content = @"";
    [_requestDataSource addObject:pa];
    
    pa = [DMGParameter new];
    pa.name = @"size";
    pa.prop = @"NSString";
    pa.content = @"";
    [_requestDataSource addObject:pa];
    
    self.responseDataSource = [NSMutableArray new];
    pa = [DMGParameter new];
    pa.name = @"start";
    pa.prop = @"NSString";
    pa.content = @"";
    [_responseDataSource addObject:pa];
    
    self.requestTableView.delegate = self;
    self.requestTableView.dataSource = self;
    
    self.responseTableView.delegate = self;
    self.responseTableView.dataSource = self;
    //_filePath = @"/Users/goldwallet/Desktop/codeTemp.h";
    _filePath = @"";
//    _targetFileTF.stringValue = _filePath;
//    _requestNameTF.stringValue = @"";
    
//    [_comboBox addItemsWithObjectValues:@[@"DepositInvest",@"OrderQuery"]];
//    [_comboBox selectItemAtIndex:0];
    
    _modelArray = [NSMutableArray new];
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

+ (NSString *)docPath
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}


- (IBAction)btnClick:(id)sender {

    _apiName = [_requestNameTF stringValue];
    
    NSButton *button = (NSButton *)sender;
    if (button.tag == view_tag_addrequest) {
        
        [self writeFile];
    } else if (button.tag == view_tag_selectfile) {
        //选择文件
        [self selectFile];
    } else if (button.tag == view_tag_addmodel) {
        ModelGenerator *generator = [[ModelGenerator alloc] init];
        [generator generateModelWithJsonStr:_textView.string modelName:_apiName toTilePath:_filePath];
    } else if (button.tag == view_tag_vcmodel) {
        //添加试图控制器模板
        YYIVCTemplateViewController *vc = [[YYIVCTemplateViewController alloc] init];
        [self presentViewControllerAsModalWindow:vc];
    } else if (button.tag == view_tag_addmuban) {
    
    } else if (button.tag == view_tag_addcell) {
        
        YYICellGeneratorViewController *vc = [[YYICellGeneratorViewController alloc] init];
        [self presentViewControllerAsModalWindow:vc];
    } else {
        
    }
}
//GetFinance
- (void)writeFile{

    _filePath = _targetFileTF.stringValue;
    
    NSString *markStr = _descriptionTF.stringValue;
    _markMacro = [NSString stringWithFormat:@"\n\n#pragma mark - %@\n\n",markStr];
    
//    NSLog(@"%@",[_filePath lastPathComponent]);
//    NSLog(@"%@",[_filePath pathExtension]);
//    NSLog(@"%@",[_filePath stringByDeletingPathExtension]);
//    NSLog(@"%@",[_filePath stringByDeletingLastPathComponent]);
//    NSLog(@"%@",[[_filePath lastPathComponent] stringByDeletingPathExtension]);
    NSString *url = _urlTF.stringValue;
    
    // 获得文件名（不带后缀）
    _superCla = [[_filePath lastPathComponent] stringByDeletingPathExtension];
    NSLog(@"_superCla：%@",_superCla);
    
    APIGenerator *api = [[APIGenerator alloc] init];
    
    [api yyi_writeAPIWithApiName:_apiName url:url superCls:_superCla parameterArray:self.requestDataSource toFilePath:_filePath functionDesi:markStr isResponse:_textView.string.length > 0];
}

- (void)selectFile{
    //选择文件
    NSOpenPanel* panel = [NSOpenPanel openPanel];
    [panel beginWithCompletionHandler:^(NSInteger result) {
        if (result == NSModalResponseOK) {
            NSURL* elemnet = panel.URLs.firstObject;
            _filePath = elemnet.path;
            _targetFileTF.stringValue = _filePath;
        }
    }];

}

#pragma mark - NSTableViewDataSource

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    if (tableView.tag%2 == 0) {
        return self.requestDataSource.count;
    }else{
        return self.responseDataSource.count;
    }
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    
    NSMutableArray *dataSource;
    if (tableView.tag%2 == 0) {
        dataSource = self.requestDataSource;
    }else{
        dataSource = self.responseDataSource;
    }
    
    DMGParameter *rowInfoDic = dataSource[row];
    if (rowInfoDic.name) {
        NSLog(@"rowInfoDic.name:  %@",rowInfoDic.name);
    } else {
        NSLog(@"rowInfoDic.name:  nil");
    }
    NSString *rowData = [rowInfoDic valueForKey:tableColumn.identifier];
    return rowData;
}




- (void)tableView:(NSTableView *)tableView setObjectValue:(nullable id)object forTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row{
    
    NSMutableArray *dataSource;
    if (tableView.tag%2 == 0) {
        dataSource = self.requestDataSource;
    }else{
        dataSource = self.responseDataSource;
    }
//    [object stringValue];
    DMGParameter *rowInfoDic = dataSource[row];
    
    [rowInfoDic setValue:[NSString stringWithFormat:@"%@",object] forKey:tableColumn.identifier];
    NSLog(@"identifier:%@   object:%@",tableColumn.identifier,object);
}

//添加
- (IBAction)addRow:(NSButton *)sender {
    NSButton *button = (NSButton *)sender;
    NSMutableArray *dataSource;
    NSTableView *tableView;
    if (button.tag%2 == 0) {
        dataSource = self.requestDataSource;
        tableView = self.requestTableView;
    }else{
        dataSource = self.responseDataSource;
        tableView = self.responseTableView;
    }
    
    DMGParameter *pa = [DMGParameter new];
    pa.name = @"start";
    pa.prop = @"NSString";
    pa.content = @"";
    [dataSource addObject:pa];
    [tableView reloadData];
    [tableView deselectAll:nil];
    [tableView editColumn:0 row:dataSource.count - 1 withEvent:nil select:YES];
}



//删除
- (IBAction)removeRow:(NSButton *)sender {
    NSButton *button = (NSButton *)sender;
    NSMutableArray *dataSource;
    NSTableView *tableView;
    if (button.tag%2 == 0) {
        dataSource = self.requestDataSource;
        tableView = self.requestTableView;
    }else{
        dataSource = self.responseDataSource;
        tableView = self.responseTableView;
    }

    NSInteger row = tableView.selectedRow;
    if (row < 0) {
        return;
    }
    [dataSource removeObjectAtIndex:row];
    [tableView reloadData];
}


#pragma mark - NSComboBoxDelegate



#pragma mark - NSComboBoxDataSource

@end
